﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Xtee.CodeDom
{
    public static class StringExtensions
    {
        public static ConcurrentDictionary<string, string> values = new ConcurrentDictionary<string, string>();
        /// <summary>
        /// Pascal case
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Pascalize(this string value)
        {
            if (string.IsNullOrEmpty(value)) return value;
            if (values.ContainsKey(value)) return values[value];

            var old = value;
            value = value.Trim();
            value = CharToUpperAfterSymbol(value, '_');
            value = CharToUpperAfterSymbol(value, ' ');
            value = AddFirstLetterCharIfStartsWithNumber(value);
            value = RemoveSpecialCharacters(value);
            values.TryAdd(old, value);
            return value;
        }

        private static string RemoveSpecialCharacters(string name)
        {
            MatchCollection coll = Regex.Matches(name, "[a-zA-Z0-9äÄöÖüÜÕõ]");
            var sb = new StringBuilder(coll.Count);
            foreach (Match var in coll)
            {
                sb.Append(var.Value);
            }

            name = sb.ToString();
            return name;
        } 
        private static string AddFirstLetterCharIfStartsWithNumber(string name)
        {
            string fLetter = name.Substring(0, 1).ToUpper();
            if (char.IsNumber(fLetter, 0))
            {
                fLetter = "N" + fLetter;
            }
            name = name.Remove(0, 1);
            name = name.Insert(0, fLetter);
            return name;
        } 
        private static string CharToUpperAfterSymbol(string value, char symbol)
        {
            while (value.IndexOf(symbol) != -1)
            {
                if (value.IndexOf(symbol) > -1 && value.IndexOf(symbol) + 1 < value.Length)
                {
                    int startIndx = value.IndexOf(symbol) + 1;

                    string letterAfterSymbol = value.Substring(startIndx, 1);
                    letterAfterSymbol = letterAfterSymbol.ToUpper();
                    value = value.Remove(startIndx - 1, 2);
                    value = value.Insert(startIndx - 1, letterAfterSymbol);

                }
            }
            return value;
        } 
    }
}
